package model.events

import model.postgresql.{PgBaseEvent, PgWallReplyNew}
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder}
import zio.json.*

import java.time.ZonedDateTime

case class WallReplyNew(
    `object`: WallReplyNew.`object`,
    event_id: Option[String],
    timestamp: Option[Long]
) extends BaseEvent(event_id, timestamp) {
  override def convertToJson: String                = this.toJson
  override def getUserId: Some[Long]                = Some(this.`object`.from_id)
  override def setTimestamp(ts: Long): WallReplyNew = copy(timestamp = Some(ts))

  override def toPg: Option[PgBaseEvent] =
    for {
      eventId   <- event_id
      timestamp <- timestamp
    } yield PgWallReplyNew(
      eventId,
      timestamp,
      `object`.text,
      `object`.id,
      `object`.from_id,
      `object`.post_id
    )
}

case class Thread(count: Long)

object WallReplyNew {

  case class `object`(
      id: Long,
      from_id: Long,
      date: Long,
      text: String,
      can_delete: Long,
      post_id: Long,
      owner_id: Long,
      parents_stack: Array[Int],
      thread: Thread,
      post_owner_id: Long
  )

  given JsonDecoder[Thread] = DeriveJsonDecoder.gen[Thread]
  given JsonEncoder[Thread] = DeriveJsonEncoder.gen[Thread]

  given JsonDecoder[`object`] = DeriveJsonDecoder.gen[`object`]
  given JsonEncoder[`object`] = DeriveJsonEncoder.gen[`object`]

  given JsonDecoder[WallReplyNew] = DeriveJsonDecoder.gen[WallReplyNew]
  given JsonEncoder[WallReplyNew] = DeriveJsonEncoder.gen[WallReplyNew]
}
